import os
import re

def list_folders(base_dir):
    return [name for name in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, name))]

def rename_files_in_folder(folder_path):
    folder_name = os.path.basename(folder_path)
    for file in os.listdir(folder_path):
        # 匹配以“文件夹名”开头，后面跟任意数量的分隔符（空格、-、_、.、冒号等）
        pattern = r'^' + re.escape(folder_name) + r'[\s\-_.:：]*'
        new_name = re.sub(pattern, '', file)
        if new_name != file and new_name:  # 避免空文件名
            old_path = os.path.join(folder_path, file)
            new_path = os.path.join(folder_path, new_name)
            os.rename(old_path, new_path)
            print(f"已重命名: {old_path} -> {new_path}")

if __name__ == "__main__":
    base_dir = "downloads"
    folders = list_folders(base_dir)
    if not folders:
        print("未找到需要处理的文件夹。")
        exit(0)
    print("请选择要处理的文件夹：")
    for idx, folder in enumerate(folders, 1):
        print(f"{idx}. {folder}")
    choice = input("输入序号：")
    try:
        choice_idx = int(choice) - 1
        if 0 <= choice_idx < len(folders):
            target_folder = os.path.join(base_dir, folders[choice_idx])
            rename_files_in_folder(target_folder)
            print("重命名完成！")
        else:
            print("无效的选择。")
    except ValueError:
        print("请输入有效的数字。") 